﻿using System;
using ExtraClub.Domain;
using ExtraClub.Domain.Entities;
using SubSonic;
using System.Collections.Generic;
using ExtraClub.Domain.Forms;
using System.Globalization;

namespace ExtraClub.Web.Forms.mailer_filter {
    public partial class module : BaseFilter {
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
        }

        protected override string SetFields() {
            return BuildFilterForm();
        }

        private string BuildFilterForm() {
            string key = "mailer_grid_filter";
            Dictionary<string, object> data = (Dictionary<string, object>)Session[key];

            FieldCollection fs = new FieldCollection();
            fs.Control = this;
            fs.LabelKeySuffex = "-label";

            AddHiddenFieldes(ref fs, "");

            /* User Name */
            new Field(FieldType.String, MailJob.Columns.Subject.ToLower(), f =>
            {
                f.Size = 50;
                f.Value = data != null ? data[f.CodeName].ToString() : "";
            }, fs);

            /* Created Period */
            new Field(FieldType.Combined, MailJob.Columns.Created.ToLower(), f =>
            {
                f.Fields = new Field[2];

                /* From */
                new Field(FieldType.DateSplit, null, a =>
                {
                    string date  =((object[])data[MailJob.Columns.Created.ToLower()])[0].ToString(); // d/m/y
                    a.Value = data != null ? DateTime.Parse(date, CultureInfo.CreateSpecificCulture("fr"), DateTimeStyles.AdjustToUniversal).ToString() : "";
                    a.DateFormat = "d/m/Y";

                    f.Fields[0] = a;
                }, null);

                /* To */
                new Field(FieldType.DateSplit, null, a =>
                {
                    a.DateFormat = "d/m/Y";
                    a.Separators = new string[] { "::" };
                    string date = ((object[])data[MailJob.Columns.Created.ToLower()])[1].ToString(); // d/m/y
                    a.Value = data != null ? DateTime.Parse(date, CultureInfo.CreateSpecificCulture("fr"), DateTimeStyles.AdjustToUniversal).ToString() : "";
                    f.Fields[1] = a;
                }, null);

            }, fs);

            return fs.JSON();

        }
    }
}
